﻿using ACGN.Application.Dtos;
using ACGN.Core.Models;
using ACGN.Core.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using ACGN.Application.ProPictures.Dtos;
using ACGN.Application.ProPictures;
using ACGN.Application.Products.Dtos;

namespace ACGN.Application.Products
{
    public class ProPictureService : IProPictureService
    {
        private readonly IRepository<ProPicture, int> _repository;

        public ProPictureService(IRepository<ProPicture, int> repository)
        {
            _repository = repository;
        }
        public async Task<PagedResultDto<ProPicture>> GetPageResult(ProPictureInputDto input)
        {
            var query = _repository.GetAll();
            if (!string.IsNullOrEmpty(input.PPName))
            {
                query = query.Where(s => s.PPName.Contains(input.PPName));
            }
            if (input.ProductId != 0)
            {
                query = query.Where(s => s.ProductId == input.ProductId);
            }
            var count = query.Count();
            query = query.OrderBy(input.Sorting).Skip((input.Page - 1) * input.Limit).Take(input.Limit);
            var stus = await query.Include(s => s.Product).AsNoTracking().ToListAsync();//不对实体类进行追踪
            var dtos = new PagedResultDto<ProPicture>
            {
                TotalCount = count,
                Page = input.Page,
                Limit = input.Limit,
                Data = stus,
                FilterText = input.FilterText,
                Sorting = input.Sorting
            };
            return dtos;
        }
    }
}
